package com.example.travel.Interface.Implements;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.example.travel.Interface.UserInterface;
import com.example.travel.bean.User;
import com.example.travel.db.DatabaseHelper;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by DELL on 2020/10/28.
 */

public class UserInterfaceImpl implements UserInterface {
    public static SQLiteDatabase dbInstance;
    Context context;

    public UserInterfaceImpl(Context context){
        this.context=context;
        dbInstance= DatabaseHelper.openDatabase(this.context);
    }
    @Override
    public void insertUser(User user) {
        String sql="insert into user_table (user_name,phone) values" +
                " ('"+user.getUser_name()+"','"+user.getPhone()+"')";
        dbInstance.execSQL(sql);
    }

    @Override
    public void deleteUserById(int user_id) {
        String sql="delete from user_table where user_id="+user_id;
        dbInstance.execSQL(sql);

    }

    @Override
    public void updateUser(User user) {
        String sql="update user_table set user_name='"+user.getUser_name()
                +"',phone='"+user.getPhone()+"' where user_id="+user.getUser_id();
        dbInstance.execSQL(sql);
    }

    @Override
    public User getUserById(int user_id) {
        User user=null;
        String sql="select * from user_table where user_id="+user_id;
        Cursor cursor=null;
        cursor=dbInstance.rawQuery(sql,null);
        while(cursor.moveToNext()){
            user=new User();
            user.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
            user.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            user.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
            user.setImage_name(cursor.getString(cursor.getColumnIndex("image_name")));
        }
        return user;
    }

    @Override
    public ArrayList<HashMap<String, Object>> getAllUsers() {
        ArrayList<HashMap<String, Object>> userList=new ArrayList<HashMap<String, Object>>();
        String sql="select * from user_table";
        Cursor cursor=dbInstance.rawQuery(sql,null);
        while (cursor.moveToNext()){
            HashMap<String, Object> item=new HashMap<String, Object>();
            item.put("user_id",cursor.getInt(cursor.getColumnIndex("user_id")));
            item.put("user_name",cursor.getString(cursor.getColumnIndex("user_name")));
            item.put("phone",cursor.getString(cursor.getColumnIndex("phone")));
            item.put("image_name",cursor.getString(cursor.getColumnIndex("image_name")));
            userList.add(item);
        }
        return userList;
    }

    @Override
    public ArrayList<HashMap<String, Object>> getAllUsersByName(String userName) {
        ArrayList<HashMap<String, Object>> userList=new ArrayList<HashMap<String, Object>>();
        String sql="select * from user_table where user_name like '%"+userName+"%'";
        Cursor cursor=dbInstance.rawQuery(sql,null);
        while (cursor.moveToNext()){
            HashMap<String, Object> item=new HashMap<String, Object>();
            item.put("user_id",cursor.getInt(cursor.getColumnIndex("user_id")));
            item.put("user_name",cursor.getString(cursor.getColumnIndex("user_name")));
            item.put("phone",cursor.getString(cursor.getColumnIndex("phone")));
            item.put("image_name",cursor.getString(cursor.getColumnIndex("image_name")));
            userList.add(item);
        }
        return userList;
    }
}

